package com.mytrip.util;

import java.util.LinkedList;
import java.util.List;

public class PageInfo<T> {
	// 当前页
	private int currentPage = 1;

	// 每页数
	private int pageSize = 5;

	// 数据列表
	private List<T> dataList = new LinkedList<T>();

	// 总长度
	private int allSize = 0;

	private String url;

	private String paramName = "page";

	public PageInfo(){

	}

	public PageInfo(int pageSize){
		this.pageSize = pageSize;
	}

	public int getCurrentPage(){
		return currentPage;
	}

	public void setCurrentPage(int currentPage){
		this.currentPage = currentPage;
	}

	public int getPageSize(){
		return pageSize;
	}

	public void setPageSize(int pageSize){
		this.pageSize = pageSize;
	}

	public List<T> getDataList(){
		return dataList;
	}

	public void setDataList(List<T> dataList){
		this.dataList = dataList;
	}

	public int getAllSize(){
		return allSize;
	}

	public void setAllSize(int allSize){
		this.allSize = allSize;
		this.currentPage = Math.min(this.currentPage, getPageCnt());
	}

	public int getFrom(){
		return (currentPage - 1) * pageSize;
	}

	public int getPageCnt(){
		return Math.max((allSize + pageSize - 1) / pageSize, 1);
	}

	public String getUrl(){
		return url;
	}

	public void setUrl(String url){
		this.url = url;
	}

	public String getParamName(){
		return paramName;
	}

	public void setParamName(String paramName){
		this.paramName = paramName;
	}

	public String getHtml(){
		currentPage = Math.min(currentPage, getPageCnt());
		StringBuilder sb = new StringBuilder(128);
		sb.append("<div style=\"float:right;\">");
		if (currentPage != 1) {
			sb.append("&nbsp;<a href=\"javascript:void(0);\" onclick=\"javascript:goPage(1);\">首页</a>");
		}
		if (currentPage > 1) {
			sb.append("&nbsp;<a href=\"javascript:void(0);\" onclick=\"javascript:goPage(" + (currentPage - 1)
					+ ");\">上一页</a>");
		}
		if (currentPage < getPageCnt()) {
			sb.append("&nbsp;<a href=\"javascript:void(0);\" onclick=\"javascript:goPage(" + (currentPage + 1)
					+ ");\">下一页</a>");
		}
		if (currentPage != getPageCnt()) {
			sb.append("&nbsp;<a href=\"javascript:void(0);\" onclick=\"javascript:goPage(" + getPageCnt()
					+ ");\">末页</a>");
		}
		sb.append("</div>");
		sb.append("<script type=\"text/javascript\">var goPage=function(a){window.location.href='" + url
				+ (url.indexOf("?") == -1 ? "?" : "&") + paramName + ".currentPage='+a;}</script>");
		return sb.toString();
	}

}
